<?php

namespace app\common\model;

use app\common\controller\Helper;
use app\common\enums\StatusEnum;
use think\Model;
use think\Db;

/**
 * This is the model class for table "oauth".
 * @property int $id 自增ID
 * @property string $openid openid
 * @property string $unionid unionid
 * @property int $parent_id 父级id
 * @property string $share_key 分享唯一key
 * @property string $nickname 昵称
 * @property string $phone 用户绑定的手机号（国外手机号会有区号）
 * @property string $pure_phone 没有区号的手机号
 * @property int $integral 积分
 * @property int $sex 用户的性别，1男性，2女性，0未知
 * @property string $province 省份
 * @property string $city 城市
 * @property string $country 国家
 * @property string $head_img 头像
 * @property string $signature 个性签名
 * @property int $autoplay 是否自动播放，1是，0否
 * @property int $status 状态 0禁用，1启用
 * @property int $create_at 添加时间
 * @property int $update_at 修改时间
 */
class Cache extends Model
{
    use Helper;

    protected $table = 'cache_list';
    

    /**
     * 通过语种id获取用户数据
     * @param $phone
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\Exception\DbException
     */
    public static function getCacheId($data)
    {
        return self::where([
                'oauth_id' => $data['uid'],
                'class_file_id' => $data['file_id'],
                'status' => 1
        ])->find();
    }
    
    /**
     * 通过语种id获取用户数据
     * @param $phone
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\Exception\DbException
     */
    public static function getCacheUid($oauthId)
    {
        return self::field('oauth_id AS uid,name,lang_id,class_id,icon,type,course_type AS courseType,SUM(CASE WHEN file_type = 1 THEN 1 ELSE 0 END) AS video_num,SUM(CASE WHEN file_type = 2 THEN 1 ELSE 0 END) AS audio_num,	SUM(CASE WHEN file_type = 3 THEN 1 ELSE 0 END) AS word_num,  SUM(CASE WHEN file_type = 4 THEN 1 ELSE 0 END) AS sentence_num')->where([
                'oauth_id' => $oauthId
        ])->group('oauth_id,type')->select()->toArray();
    }
    
    
    /**
     * 通过语种id获取用户数据
     * @param $phone
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\Exception\DbException
     */
    public static function getOneList($data)
    {
        
        return self::field('oauth_id AS uid,name,lang_id,class_id,icon,type,SUM(CASE WHEN file_type = 1 THEN 1 ELSE 0 END) AS video_num,SUM(CASE WHEN file_type = 2 THEN 1 ELSE 0 END) AS audio_num,	SUM(CASE WHEN file_type = 3 THEN 1 ELSE 0 END) AS word_num,  SUM(CASE WHEN file_type = 4 THEN 1 ELSE 0 END) AS sentence_num')
        ->where([
            'oauth_id' => $data['uid'],
            'type' => $data['type'],
            
        ])->group('oauth_id,type')->select()->toArray();
    }
    
    
    /**
     * 获取文件
     * @param $phone
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\Exception\DbException
     */
    public static function getLangClassFile($data,$type)
    {
    
        //判断缓存的file_id
        $retdata =  Db::table('lang_class_file')->field('a.class_id AS classId,a.type,a.size,a.file,a.file_name AS fileName,a.video_img AS videoImg,a.create_time,o.cache_id AS cacheId ')
                    ->alias('a')
                    ->join('cache_list o', 'a.id = o.class_file_id')
                    ->where([
            'a.class_id' => $data['class_id'],
            'a.type' => $type
            ])->order('create_time DESC')->select();
        if($retdata){
            foreach($retdata as $k=>$v ){
                $retdata[$k]['file'] = config('file_prefix').$v['file'];
                $retdata[$k]['videoImg'] = config('file_prefix').$v['videoImg'];
                $retdata[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }
        return $retdata;
    }
    
    
    /**
     * 获取套题
     * @param $phone
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\Exception\DbException
     */
    public static function getReciteType($data,$type)
    {
        $file_type = $type==1?3:4;
        //判断缓存的file_id
        $retdata =  Db::table('lang_class_recite_type')->field('a.id AS file_id,a.size,a.class_id AS classId,a.type,a.name,a.course_type AS courseType,a.create_time,o.cache_id AS cacheId')
        ->alias('a')
        ->join('cache_list o', 'a.id = class_file_id  AND a.course_type = o.course_type AND a.class_id = o.class_id ')
        ->where([
            'a.class_id' => $data['class_id'],
            'a.type' => $type,
            'o.file_type' => $file_type
        ])
        ->order('create_time DESC')->select();
        if($retdata){
            foreach($retdata as $k=>$v ){
                $retdata[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }
        return $retdata;

    }
}